package yxy.game.pm2.command.fortdefeat;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.running;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FormatMessage;
import yxy.game.pm2.message.FortDefeatMessage;
import yxy.game.pm2.module.fortdefeat.FortDefeatHandle;
import yxy.game.pm2.module.fortdefeat.FortDefeatModule;

/** 副本过关斩将 过关斩将快速挑战 */
@MessageType(proto = Command.FortDefeatChanllengeQuick, classtype = FortDefeatMessage.FortDefeatChanllengeQuickRequest.class)
public class CMD_FortDefeatChanllengeQuick extends Command<FortDefeatMessage.FortDefeatChanllengeQuickRequest> {

	@Override
	public void execute(MessageSource messageSource, FortDefeatMessage.FortDefeatChanllengeQuickRequest request) {
		String playerCode = messageSource.getTag();
		int mapId = request.getFortId();
		FormatMessage.Format formatrequest = request.getFormat();

		FortDefeatMessage.FortDefeatChanllengeQuickResponse.Builder response = FortDefeatMessage.FortDefeatChanllengeQuickResponse.newBuilder();

		FortDefeatHandle handle = new FortDefeatHandle();
		if (handle.isBattling(playerCode)) {
			send(messageSource, response, Result.BattleIsRunning);// 还有正在进行中的战斗
			return;
		}

		// 判断参数
		running _data = DataCenter.getData(mapId, running.class);
		if (_data == null) {
			send(messageSource, response, Result.ParamError);// 数据不存在
			return;
		}
		FortDefeatModule defeatModule = FortDefeatModule.get(playerCode);
		if (defeatModule.getType() == null || defeatModule.getType().getNumber() != _data.difficulty) {
			send(messageSource, response, Result.FortDefeatDifficultError);// 关卡难度错误
			return;
		}
		if (defeatModule.getMapId() == 0 && mapId != defeatModule.getType().getStartId()) {
			send(messageSource, response, Result.FortDefeatStageError);// 关卡Id错误
			return;
		}
		if (defeatModule.getMapId() > 0 && mapId != defeatModule.getMapId() + 1) {
			send(messageSource, response, Result.FortDefeatStageError);// 关卡Id错误
			return;
		}
		try {
			Result result = handle.checkFormat(playerCode, formatrequest);
			if (!result.equals(Result.Success)) {
				send(messageSource, response, result);
				return;
			}
			result = handle.checkFormatCanBattle(playerCode);
			if (!result.equals(Result.Success)) {
				send(messageSource, response, result);
				return;
			}

			response.setBattleResp(handle.battleResponseExecuteQuick(playerCode, mapId));
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, FortDefeatMessage.FortDefeatChanllengeQuickResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}